<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-col :span="8">
            <el-form-item label="源币种" prop="source_currency">
              <el-select v-model="form.source_currency" placeholder="数据类型" clearable style="width: 100%">
                <el-option
                  v-for="(dict,index) in currencyAll"
                  :key="index"
                  :label="dict.currency_num"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="目标币种" prop="target_currency">
              <el-select v-model="form.target_currency" placeholder="目标币种" clearable style="width: 100%">
                <el-option
                  v-for="(dict,index) in currencyAll"
                  :key="index"
                  :label="dict.currency_num"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="汇率" prop="rate">
              <el-input v-model="form.rate" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="生效日期" prop="start_date">
              <el-date-picker v-model="form.start_date" value-format="yyyy-MM-dd" type="date" placeholder="生效日期" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="失效日期" prop="end_date">
              <el-date-picker v-model="form.end_date" value-format="yyyy-MM-dd" type="date" placeholder="失效日期" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { exchangerateAdd, exchangerateEdit } from '@/api/basics'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    },
    currencyAll: {
      type: Array
    }
  },
  data() {
    return {
      rules: {
        source_currency: [
          { required: true, message: '请选择源币种', trigger: 'change' }
        ],
        target_currency: [
          { required: true, message: '请选择目标币种', trigger: 'change' }
        ],
        rate: [
          { required: true, message: '请填写汇率', trigger: 'blur' }
        ],
        start_date: [
          { required: true, message: '请选择生效日期', trigger: 'change' }
        ],
        end_date: [
          { required: true, message: '请选择失效日期', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      exchangerateAdd(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      exchangerateEdit(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    }
  }
}
</script>

